/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.orderoid.ejb.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.orderoid.ejb.domain.Employe;
import org.orderoid.ejb.domain.Restaurant;
import org.orderoid.ejb.exceptions.MetierException;
import org.orderoid.ejb.exceptions.TechniqueException;

/**
 *
 * @author Dahlia
 */
@Stateless
public class EmployeFacade extends AbstractFacade<Employe> {
    @PersistenceContext(unitName = "Orderoid-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EmployeFacade() {
        super(Employe.class);
    }
    
    public Employe findByLogin(String login){
        Query query = em.createQuery("SELECT e FROM Employe e WHERE e.login = :login");
        query.setParameter("login", login);
        List<Employe> employes = query.getResultList();
        if(employes == null || employes.size() != 1){
            return null;
        }
        return (Employe) query.getResultList().get(0);
    }
    
    public Employe checkPassword(String identifiant, String password) 
            throws TechniqueException, MetierException{
        
        // On teste si les champs sont nulles
        if(identifiant == null || password == null){
            throw new TechniqueException("Veuillez remplir les champs obligatoires.");
        }
        //TODO
        // Récupération du client
        Employe employeBase = findByLogin(identifiant);
        
        // On teste si le client existe et que le mot de passe est bon
        if(employeBase != null && employeBase.getPassword().equals(password)){
            return employeBase;
        }else{
            throw new MetierException("Le mot de passe ou l'identifiant est incorrect.");
        }
    }
    
    /**
     * Récupère l'identifiant du restaurant de l'employé
     * @param login Le login de l'employé
     * @return L'identifiant du restaurant
     */
    /*
    public int getIdRestaurant(String login)
    {
        Query query = em.createQuery("SELECT idRestaurant FROM Employe e WHERE e.login = :login");
        query.setParameter("login", login);
        
        int idRestaurant = query.getFirstResult();
        
        return idRestaurant;
    }
    * 
    */
}
